package com.jc.demo.hadoop.jdata;

import com.jc.demo.hadoop.yarn.mr.MaxTemperatureReducer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import org.apache.orc.mapred.OrcStruct;
import org.apache.orc.mapreduce.OrcInputFormat;
import org.apache.orc.mapreduce.OrcOutputFormat;

/**
 * 在命令行启动时，如：
 * hadoop jar jc-demo-hadoop-1.0.0.0-SNAPSHOT-jar-with-dependencies.jar com.jc.demo.hadoop.jdata.BlankPhoneDriver /user/hive/warehouse/sms_raw.db/t_sms_access_orc_bu /user/testorc3
 * -conf就可以动态配置参数，默认则从$HADOOP_HOME/etc/hadoop里取配置
 */
public class BlankPhoneDriver extends Configured implements Tool {
    @Override
    public int run(String[] args) throws Exception {
        if (args.length != 2) {
            System.err.printf("Usage: %s [generic options] <input> <output> \n", getClass().getSimpleName());
            ToolRunner.printGenericCommandUsage(System.err);
            return -1;
        }



        Configuration conf = new Configuration();

        String s = conf.get("-libjars");//可以用于查看使用libjars参数引入的jar文件
        System.out.println("tmpjars:"+s);

        String s2 = conf.get("libjars");//可以用于查看使用libjars参数引入的jar文件
        System.out.println("libjars:"+s2);

        conf.set("orc.mapred.output.schema","struct<phone:string,firsDate:string,lastDate:string>");
        Job job = Job.getInstance(conf, "ORC Test");
        job.setJarByClass(BlankPhoneDriver.class);
        job.setMapperClass(BlankPhoneMapper.class);
        job.setCombinerClass(BlankPhoneReducer.class);
        job.setReducerClass(BlankPhoneReducer.class);
        job.setInputFormatClass(OrcInputFormat.class);
        job.setOutputFormatClass(OrcOutputFormat.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);
        job.setOutputKeyClass(NullWritable.class);
        job.setOutputValueClass(OrcStruct.class);
        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileInputFormat.setInputDirRecursive(job,true);
        FileOutputFormat.setOutputPath(job, new Path(args[1]));


        return job.waitForCompletion(true) ? 0 : 1;
    }

    public static void main(String[] args) throws Exception {
        int exitCode = ToolRunner.run(new BlankPhoneDriver(), args);
        System.exit(exitCode);
    }
}
